package org.jeecg.modules.hc.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class HcUtils {

    public static JSONObject getYongShanTuanTime(String date) {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar instance = Calendar.getInstance();

        String beginTime = "";
        String endTime = "";

        try {
            instance.setTime(dateFormat1.parse(date));
            endTime = dateFormat1.format(instance.getTime()) + " 18:00:00";
            instance.add(Calendar.DATE, -1);
            beginTime = dateFormat1.format(instance.getTime()) + " 18:00:01";
        } catch (ParseException e) {
            log.error("日期格式化错误");
            e.printStackTrace();
        }
//        try {
//            instance.setTime(dateFormat1.parse(date));
//            endTime = dateFormat1.format(instance.getTime()) + " 23:59:59";
//            instance.add(Calendar.DATE, -1);
//            beginTime = dateFormat1.format(instance.getTime()) + " 00:00:01";
//        } catch (ParseException e) {
//            log.error("日期格式化错误");
//            e.printStackTrace();
//        }

        JSONObject result = new JSONObject();
        result.put("beginTime", beginTime);
        result.put("endTime", endTime);
        return result;
    }

    public static JSONObject getYongShanTuanTime() {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar instance = Calendar.getInstance();

        String nowDate = dateFormat1.format(instance.getTime());
        String beginTime = "";
        String endTime = "";

        try {
            if (new Date().before(dateFormat2.parse(nowDate + " 18:00:00"))) {
                endTime = dateFormat1.format(instance.getTime()) + " 18:00:00";
                instance.add(Calendar.DATE, -1);
                beginTime = dateFormat1.format(instance.getTime()) + " 18:00:01";
            } else {
                beginTime = dateFormat1.format(instance.getTime()) + " 18:00:01";
                instance.add(Calendar.DATE, 1);
                endTime = dateFormat1.format(instance.getTime()) + " 18:00:00";
            }
        } catch (ParseException e) {
            log.error("日期格式化错误");
            e.printStackTrace();
        }

        JSONObject result = new JSONObject();
        result.put("beginTime", beginTime);
        result.put("endTime", endTime);
        return result;
    }

    public static MultipartFile workbookToCommonsMultipartFile(Workbook workbook, String fileName) {
        //Workbook转FileItem
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem fileItem = factory.createItem("textField", "text/plain", true, fileName);
        try {
            OutputStream os = fileItem.getOutputStream();
            workbook.write(os);
            os.close();
            //FileItem转MultipartFile
            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
            return multipartFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean nowBetweenHHmmss(String timeBegin, String timeEnd) {
        if (StringUtils.isBlank(timeBegin) || StringUtils.isBlank(timeEnd)) {
            return false;
        }

        String datePrefix = "2022-12-12";
        String datePrefix1 = "2022-12-11";
        String datePrefix2 = "2022-12-13";
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (timeBegin.split(":").length == 2) {
            timeBegin = timeBegin + ":00";
        }

        if (timeEnd.split(":").length == 2) {
            timeEnd = timeEnd + ":00";
        }

        try {
            Date parseBegin = dateFormat2.parse(datePrefix + " " + timeBegin);
            Date parseEnd = dateFormat2.parse(datePrefix + " " + timeEnd);
            Date now = dateFormat2.parse(datePrefix + " " + dateFormat1.format(new Date()));

            //如果结束时间大于开始时间，需要计算隔天范围
            if (parseEnd.before(parseBegin)) {
                //先把开始日期减一天去判断
                parseBegin = dateFormat2.parse(datePrefix1 + " " + timeBegin);

                if (now.after(parseBegin) && now.before(parseEnd)) {
                    return true;
                } else {
                    //如果不在范围内  则把结束日期加一天去判断
                    parseBegin = dateFormat2.parse(datePrefix + " " + timeBegin);
                    parseEnd = dateFormat2.parse(datePrefix2 + " " + timeEnd);

                    if (now.after(parseBegin) && now.before(parseEnd)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } else {
                if (now.after(parseBegin) && now.before(parseEnd)) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}
